function parseElement(domElem, editor) {
    const attributes = JSON.parse(domElem.getAttribute('allAttributes')) || {};
    const tagName = domElem.getAttribute('myTagName') || domElem.tagName.toLowerCase();
    // 如果是 table 元素，直接返回 Slate 的 table 结构
    if (tagName === 'table' || tagName === 'tr' || tagName === 'td' || tagName === 'th') {
        const tableElement = {
            type: 'table',
            myTagName: 'table',
            allAttributes: attributes,
            children: [],
        };

        // 递归处理子节点
        const childNodes = domElem.childNodes;
        for (let i = 0; i < childNodes.length; i++) {
            const childNode = childNodes[i];
            if (
                childNode.nodeType === Node.ELEMENT_NODE &&
                (childNode.tagName.toLowerCase() === 'tr' || 
                 (childNode.tagName.toLowerCase() === 'mention' && childNode.getAttribute('myTagName') === 'tr'))
            ) {
                const rowElement = {
                    type: 'table-row',
                    myTagName: 'tr',
                    allAttributes: JSON.parse(childNode.getAttribute('allAttributes')) || {}, // 确保 tr 元素的 attributes 正确传递
                    children: [],
                    data: {
                        allAttributes: JSON.parse(childNode.getAttribute('allAttributes')) || {},
                    }
                };

                // 处理 tr 的子节点（td 或 th）
                const cellNodes = childNode.childNodes;
                for (let j = 0; j < cellNodes.length; j++) {
                    const cellNode = cellNodes[j];
                    if (
                        cellNode.nodeType === Node.ELEMENT_NODE &&
                        (cellNode.tagName.toLowerCase() === 'td' ||
                         cellNode.tagName.toLowerCase() === 'th' ||
                         (cellNode.tagName.toLowerCase() === 'mention' && 
                          (cellNode.getAttribute('myTagName') === 'td' || cellNode.getAttribute('myTagName') === 'th')))
                    ) {
                        const cellElement = {
                            type: 'table-cell', // 区分 th 和 td
                            myTagName: cellNode.tagName.toLowerCase() === 'th' || cellNode.getAttribute('myTagName') === 'th' ? 'th': 'td',
                            isHeader: cellNode.tagName.toLowerCase() === 'th' || cellNode.getAttribute('myTagName') === 'th' ? true : false,
                            allAttributes: JSON.parse(cellNode.getAttribute('allAttributes')) || {}, // 确保 td/th 元素的 attributes 正确传递
                            children: [],
                        };
                        // 处理 td 或 th 的子节点（可能是文本或其他元素）
                        const cellChildNodes = cellNode.childNodes;
                        for (let k = 0; k < cellChildNodes.length; k++) {
                            const cellChildNode = cellChildNodes[k];
                            if (cellChildNode.nodeType === Node.TEXT_NODE) {
                                const textContent = cellChildNode.nodeValue?.trim();
                                if (textContent) {
                                    cellElement.children.push({ text: textContent || '' });
                                }
                            } else if (cellChildNode.nodeType === Node.ELEMENT_NODE) {
                                // 递归处理嵌套的元素
                                const nestedElement = parseElement(cellChildNode, editor);
                                cellElement.children.push(nestedElement);
                            }
                        }

                        // 如果 cellElement 没有子节点，添加一个空文本节点
                        if (cellElement.children.length === 0) {
                            cellElement.children.push({ text: '' });
                        }
                        rowElement.children.push(cellElement);
                    }
                }
                tableElement.children.push(rowElement);
            }
        }
        return tableElement;
    } else {
        // 其他元素继续按照原来的逻辑处理
        const element = {
            type: 'mention',
            myTagName: tagName,
            allAttributes: attributes,
            children: [],
        };
        const childNodes = domElem.childNodes;
        for (let i = 0; i < childNodes.length; i++) {
            const childNode = childNodes[i];
            if (childNode.nodeType === Node.ELEMENT_NODE) {
                const childElement = parseElement(childNode, editor);
                element.children.push(childElement);
            } else if (childNode.nodeType === Node.TEXT_NODE) {
                const textContent = childNode.nodeValue?.trim();
                if (textContent) {
                    element.children.push({ text: textContent || '' });
                }
            }
        }

        if (element.children.length === 0) {
            element.children.push({ text: '' });
        }
        return element;
    }
}

const parseHtmlConf = {
    selector: 'mention',
    parseElemHtml: parseElement,
};

export default parseHtmlConf;